from django.db.utils import IntegrityError
from scrapy import log
from scrapy.exceptions import DropItem
from dynamic_scraper.models import SchedulerRuntime
from models import MotorSite, MotorSubSite


class DjangoWriterPipeline(object):
    source_model = None

    def __init__(self):
        self.urls = set([m.url for m in self.source_model.objects.all()])

    def process_item(self, item, spider):
        url = item['url']
        if url in self.urls:
            raise DropItem("Duplicate item found: %s" % item)
        try:
            item['source'] = spider.ref_object
            self.custom_item(item, spider)

            item.save()
            spider.action_successful = True
            spider.log("Item saved.", log.INFO)
        except IntegrityError as e:
            spider.log(str(e), log.ERROR)
            raise DropItem("Missing attribute.")
        self.urls.add(url)

        return item

    def custom_item(self, item, spider):
        pass


class MotorSubSiteWriterPipeline(DjangoWriterPipeline):
    source_model = MotorSite

    def custom_item(self, item, spider):
        item['name'] = '%s: %s' % (spider.ref_object.domain, item['name'])


class CarWriterPipeline(DjangoWriterPipeline):
    source_model = MotorSubSite

    def custom_item(self, item, spider):
        checker_rt = SchedulerRuntime(runtime_type='C')
        checker_rt.save()
        item['checker_runtime'] = checker_rt
